<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
declare (strict_types=1);

namespace app\api\controller;
use think\App;
use think\facade\Db;
use app\common\Baidu;

/**
 * 登录
 * Class Index
 * @package app\api\controller
 */
class Demo
{
    public function upload()
    {
      
        $param = get_params();

        if(request()->file('file')){
            $file = request()->file('file');
        }
        else{
            return json(['code' => 400, 'msg' => '未上传图片']);
        }
        // 获取上传文件的hash散列值
        $sha1 = $file->hash('sha1');
        $md5 = $file->hash('md5');
        $rule = [
            'image' => 'jpg,png,jpeg,gif',
            'doc' => 'doc,docx,ppt,pptx,xls,xlsx,pdf',
            'file' => 'zip,gz,7z,rar,tar',
            'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
        ];
        $fileExt = $rule['image'];
        //1M=1024*1024=1048576字节
        $fileSize = 100 * 1024 * 1024;
        if (isset($param['type']) && $param['type']) {
            $fileExt = $rule[$param['type']];
        }
        if (isset($param['size']) && $param['size']) {
            $fileSize = $param['size'];
        }
        $validate = \think\facade\Validate::rule([
            'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
        ]);
        $file_check['image'] = $file;
        if (!$validate->check($file_check)) {
            return json(['code' => 400, 'msg' => $validate->getError()]);
        }
        // 日期前綴
        $dataPath = date('Ym');
        $use = 'thumb';
        $filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
            return $md5;
        });

        $data = [];
        $path = get_config('filesystem.disks.public.url');
        $data['filepath'] ="https://".$_SERVER['SERVER_NAME'].$path . '/' . $filename;
        return json(['code' => 200, 'msg' => '成功', 'data' => $data]);
    }





    public function baidu_api(){
        $baidu=new Baidu();
        $param = get_params();
        $img_url=$param['img_url']??'';
        $type=$param['type']??'';
        if(!$img_url || !$type){
            return json(['code' => 400, 'msg' => '缺少必要参数']);
        }
        $res=$baidu->check_id_card($img_url,$type);
        return json(['code' => 200, 'msg' => '识别请求成功', 'data' => $res]);
    }

    public function getvdcode(){
        $param = get_params();
        $code = rand(100000, 999999);
        $content = array(
            "code" => $code,
        );
        $phone=$param['phone']??'';
        $data=['type'=>2,'mobile'=>$phone,'code'=>$code,'status'=>0,'create_time'=>time()];
        $res=smsdemo($phone, $content, 'SMS_232500146');
        if($res->Code=='OK'){
            $data['status']=1;
        }else{
            $data['status']=2;
            $data['reason']=$res->Message;
        }
        Db::name('sms_logs')->insert($data);
        if($res->Code=='OK'){
            return json(['code' => 200, 'msg' => '发送成功']);
        }else{
            return json(['code' => 400, 'msg' => '发送失败:原因'.$res->Message]);
        }
    }









}
